package com.powerunion.datacollection.web.struts;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;

/**
 * Action基类
 * 提供一些基本的简化函数
 *
 * @author qyw
 */
public class StrutsAction extends DispatchAction {

	public static final String SUCCESS = "{success:true}";
	public static final String FAILURE = "{failure:true}";

	public static final String DIRECTLY_MESSAGE_KEY = "message";
	
	/**
	 * 直接输出.
	 * 
	 * @param contentType 内容的类型.html,text,xml的值见后，json为"text/x-json;charset=UTF-8"
	 */
	protected void render(HttpServletResponse response, String text,String contentType){
		try {
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}
	/**
	 * 直接输出纯json格式字符串
	 */
	protected void renderJson(HttpServletResponse response, String text){
			this.render(response, text, "text/json;charset=utf-8");
	}
	
	/**
	 * 直接输出纯字符串.
	 */
	protected void renderText(HttpServletResponse response, String text) {
		render(response,text,"text/plain;charset=UTF-8");
	}

	/**
	 * 直接输出纯HTML.
	 */
	protected void renderHtml(HttpServletResponse response, String text) {
		render(response,text,"text/html;charset=UTF-8");
	}

	/**
	 * 直接输出纯XML.
	 */
	protected void renderXML(HttpServletResponse response, String text) {
		render(response,text,"text/xml;charset=UTF-8");
	}
	/**
	 * 直接输出代表成功的json字符串
	 */
	protected void renderSuccssJson(HttpServletResponse response){
		this.renderJson(response, SUCCESS);
	}
	/**
	 * 直接输出代表失败的json字符串
	 */
	protected void renderFAILUREJson(HttpServletResponse response){
		this.renderJson(response, FAILURE);
	}
	
	/**
	 * 直接输出代表成功的json字符串
	 */
	protected void renderSuccssJson(HttpServletResponse response,String data){
		this.renderJson(response, "{success:true,data:'"+data+"'}");
	}
	/**
	 * 直接输出代表失败的json字符串
	 */
	protected void renderFAILUREJson(HttpServletResponse response,String data){
		this.renderJson(response, "{failure:true,data:'"+data+"'}");
	}
}
